using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace TuMama
{
    public class Match
    {
        enum state { started, idle };
        private Jugador me;
        private state estado;
        public static Match thismatch;
        private ArrayList listaJugadores;
        private Quiz lista;
        private GestorConexionCliente gestor;
        public event EventHandler ListaJugadoresActualizada;
        public event EventHandler GameStarted;
        public event EventHandler QuizReceived;
        public event EventHandler ConnectionRefused;
        public event EventHandler YouWin;
        public event EventHandler YouLose;
        public event EventHandler DoLogin;
        public event EventHandler DoWaiting;
        public event EventHandler OkCancion;


        public Match()
        {
            string ip = "192.168.1.78";
            int portNo = 11000;
            this.gestor = new GestorConexionCliente(ip, portNo);
            this.gestor.RecieveCompleted += new EventHandler(gestor_RecieveCompleted);
            thismatch = this;
            estado = state.idle;
        }

        public void setNewIp(String ip)
        {
            int portNo = 11000;
            this.gestor = new GestorConexionCliente(ip, portNo);
            this.gestor.RecieveCompleted += new EventHandler(gestor_RecieveCompleted);
            thismatch = this;
            estado = state.idle;
        }

        void gestor_RecieveCompleted(object sender, EventArgs e)
        {
            Mensaje check = this.gestor.getLastReceived();
            switch (check.Message)
            {

                case Mensaje.message.sendListaJugadores:
                    this.listaJugadores = (ArrayList)check.Objeto;
                    if (ListaJugadoresActualizada != null)
                        ListaJugadoresActualizada(this, new EventArgs());
                    break;
                case Mensaje.message.start:
                    if (GameStarted != null)
                        GameStarted(this, new EventArgs());
                    break;
                case Mensaje.message.sendQuiz:
                    this.lista = (Quiz)check.Objeto;
                    if (QuizReceived != null)
                        QuizReceived(this, new EventArgs());
                    break;
                case Mensaje.message.tumama:
                    if (ConnectionRefused != null)
                        ConnectionRefused(this, new EventArgs());
                    break;
                case Mensaje.message.youLoser:
                    this.listaJugadores = (ArrayList)check.Objeto;
                    if (YouLose != null)
                        YouLose(this, new EventArgs());
                    break;
                case Mensaje.message.doLogin:
                    this.me = (Jugador)check.Objeto;
                    if (DoLogin != null)
                        DoLogin(this, new EventArgs());
                    break;
                case Mensaje.message.doWaiting:
                    if (DoWaiting != null)
                        DoWaiting(this, new EventArgs());
                    break;
                case Mensaje.message.youWinBitch:
                    this.listaJugadores = (ArrayList)check.Objeto;
                    if (YouWin != null)
                        YouWin(this,new EventArgs());
                    break;
                case Mensaje.message.okCancion:
                    if (OkCancion != null)
                        OkCancion(this, new EventArgs());
                    break;
                default:
                    break;
            }
        }

        public void Start()
        {
            this.gestor.Connect();
            this.gestor.WaitForData();
            this.estado = state.started;
        }

        public void Disconnect()
        {
            gestor.Disconnect();
        }

        public Boolean isConnected()
        {
            if (this.estado == Match.state.started)
                return true;
            return false;
        }

        public Quiz Lista
        {
            get
            {
                return this.lista;
            }
        }

        public ArrayList ListaJugadores
        {
            get
            {
                return this.listaJugadores;
            }
        }

        public String PlayerName
        {
            get
            {
                return me.NombreJugador;
            }
        }


        public void SendMessage(Mensaje nuevo)
        {
            gestor.SendMessage(nuevo);
        }
    }
}
